﻿using System.Windows;

namespace SMARTMobile.Views
{
    public partial class NoTelerikMainPage
    {
        private bool _isSidebarVisible = false;

        public NoTelerikMainPage()
        {
            InitializeComponent();
        }

        protected override void OnPageLoaded(RoutedEventArgs e)
        {
            var viewModel = SMModelLocator.Instance.NoTelerikMain;
            if (viewModel == null)
                return;

            viewModel.SidebarStateChanged += this.RaiseSidebarStateChanged;
        }

        protected override void OnPageUnloaded(RoutedEventArgs e)
        {
            var viewModel = SMModelLocator.Instance.NoTelerikMain;
            if (viewModel == null)
                return;

            viewModel.SidebarStateChanged -= this.RaiseSidebarStateChanged;
        }

        private void RaiseSidebarStateChanged(bool? sidebarState)
        {
            if (sidebarState.HasValue && sidebarState.Value)
            {
                VisualStateManager.GoToState(this, "SidebarOpenState", true);
                this._isSidebarVisible = true;
            }
            else if (this._isSidebarVisible)
            {
                VisualStateManager.GoToState(this, "SidebarClosedState", true);
                this._isSidebarVisible = false;
            }
            else
            {
                VisualStateManager.GoToState(this, "SidebarOpenState", true);
                this._isSidebarVisible = true;
            }
        }
    }
}